package Util;

import Util.HandlerDefinition;
import interceprtor.InterceptorChain;
import servlet.Dispacther;

import javax.print.DocFlavor;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class HandlerMaping {
   public InterceptorChain find(HttpServletRequest request) throws InstantiationException, IllegalAccessException{
       String url = request.getServletPath();
       HandlerDefinition handlerDefinition = getDefinition(request, url);
       InterceptorChain chain=new InterceptorChain();
        chain.setDefinition(handlerDefinition);
       return chain;
    }

    private HandlerDefinition getDefinition(HttpServletRequest request, String url) {
        ServletContext servletContext = request.getServletContext();
        Map<String,HandlerDefinition> mappings =(Map<String,HandlerDefinition>) servletContext.getAttribute(Dispacther.DEFAULT);
        return mappings.get(url);
    }
}
